const http = require('http');
const url  = require('url');
const fs   = require('fs');

const PORT = 8080
const BASEURL = 'localhost'

const start = (route, handle) => {
  /**
   * route 判断url是否存在， 存在调用handle处理，不存在返回404
   * handle 处理不同的url请求
   */

  const onRequest = (req, res)=>{
    /**
     * url.parse()解析url
     * url:string ==> Object
     */
    const pathname = url.parse(req.url).pathname
    console.log(`Request for ${pathname} received`)

    return route(handle, pathname, res, req)
  }

  http.createServer(onRequest).listen(PORT, BASEURL)
  console.log(`Server running at ${BASEURL}:${PORT}`)
}

exports.start = start